using System;
using IphoneTestNavApplication.BL;
using MonoTouch.UIKit;
using System.Collections.Generic;
	
	
namespace IphoneTestNavApplication
{

	public class DataSource : UITableViewSource
	{
		#region PRIVATE MEMBERS
		private List<Contatto> _contatti;
		
		#endregion
		
		#region PROTECTED PROPERTIES
		public List<Contatto> Contatti {
			get {
				if (_contatti == null)
					_contatti = new List<Contatto> ();
				return _contatti;
			}
		}
		#endregion
		
		#region CTOR
		public DataSource ()
		{
		}
		public DataSource (List<Contatto> contatti)
		{
			_contatti = contatti;
		}
		#endregion
		
		#region PUBLIC METHODS
		public override int NumberOfSections (UITableView tableView)
		{
			return 1;
		}

		// Customize the number of rows in the table view
		public override int RowsInSection (UITableView tableview, int section)
		{
			return this.Contatti.Count;
		}

		// Customize the appearance of table view cells.
		public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
		{
			string cellIdentifier = "BasicTableViewCell";
			var cell = tableView.DequeueReusableCell (cellIdentifier);
			if (cell == null) {
				cell = new UITableViewCell (UITableViewCellStyle.Subtitle, cellIdentifier);
			}
			Contatto c = this.Contatti[indexPath.Row];
			
			// Configure the cell.
			cell.TextLabel.Text = c.Nome + " " + c.Cognome;
			if (cell.DetailTextLabel != null)
				cell.DetailTextLabel.Text = c.NumeroTelefono;
			return cell;
		}

		/*
			// Override to support conditional editing of the table view.
			public override bool CanEditRow (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
			{
				// Return false if you do not want the specified item to be editable.
				return true;
			}
			*/
		/*
			// Override to support editing the table view.
			public override void CommitEditingStyle (UITableView tableView, UITableViewCellEditingStyle editingStyle, MonoTouch.Foundation.NSIndexPath indexPath)
			{
				if (editingStyle == UITableViewCellEditingStyle.Delete) {
					controller.TableView.DeleteRows (new NSIndexPath[] { indexPath }, UITableViewRowAnimation.Fade);
				} else if (editingStyle == UITableViewCellEditingStyle.Insert) {
					// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
				}
			}
			*/
		/*
			// Override to support rearranging the table view.
			public override void MoveRow (UITableView tableView, NSIndexPath sourceIndexPath, NSIndexPath destinationIndexPath)
			{
			}
			*/
		/*
			// Override to support conditional rearranging of the table view.
			public override bool CanMoveRow (UITableView tableView, NSIndexPath indexPath)
			{
				// Return false if you do not want the item to be re-orderable.
				return true;
			}
			*/

		// Override to support row selection in the table view.
		public override void RowSelected (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
		{
			// Navigation logic may go here -- for example, create and push another view controller.
			// var anotherViewController = new AnotherViewController ("AnotherView", null);
			//controller.NavigationController.PushViewController (anotherViewController, true);
		}
		#endregion
	}
}

